import axios from 'axios';
import qs from 'querystring';
import { invert, isEmpty, pickBy } from 'lodash';
import { downloadFile } from '@util/download-file';
import { DEVICELIST } from '@util/trans-device-name';

export const addAsset = body => {
  return axios.post('/api/site/addSite', body);
};

export const loadAssetDetail = id => {
  return axios.get(`/api/site/siteDetail?site_id=${id}`);
};

export const loadAssets = params => {
  if (params.device) {
    let device = invert(DEVICELIST)[params.device];
    if (device) {
      params.device = device;
    }
  }
  // if (params.vulStatus) {
  //   params.vulStatus.length && (params.vulStatus = JSON.stringify(params.vulStatus));
  // }
  // if (params.weightStatus) {
  //   params.weightStatus.length && (params.weightStatus = JSON.stringify(params.weightStatus));
  // }
  return axios.post('/api/site/siteList', params);
};

export const loadChangeAssets = params => {
  return axios.post('/api/site/siteChangeList', params);
};

export const batchDeleteAssets = ids => {
  return axios.post('/api/site/batchDeleteSite', {
    // eslint-disable-next-line
    site_ids: ids
  });
};

export const updateAsset = body => {
  return axios.post('/api/site/updateSite', body);
};

export const batchAddAssets = body => {
  return axios.post('/api/site/batchAddSite', body);
};

export const getAssetReportsList = () => {
  return axios.get('/api/site/showReport');
};

export const exportAssetReport = (params = {}) => {
  return axios.post('/api/site/exportReport', pickBy(params, i => !isEmpty(i)));
};

export const downloadAssetReport = filename => {
  return downloadFile('get', `/api/site/downloadReport?filename=${filename}`);
};

export const deleteAssetReport = filename => {
  return axios.post('/api/site/deleteReport', { filename });
};

export const getAssetAnalysisReportsList = () => {
  return axios.get('/api/site/showAnalysisReport');
};

export const exportAssetAnalysisReport = () => {
  return axios.get('/api/site/exportAnalysisReport');
};

export const downloadAssetAnalysisReport = filename => {
  return downloadFile('get', `/api/site/downloadAnalysisReport?filename=${filename}`);
};

export const deleteAssetAnalysisReport = filename => {
  return axios.post('/api/site/deleteAnalysisReport', { filename });
};

export const getAssetFormList = () => {
  return axios.get('/api/site/showForm');
};

export const exportAssetForm = (params = {}) => {
  return axios.post('/api/site/exportForm', pickBy(params, i => !isEmpty(i)));
};

export const exportChangeForm = (params = {}) => {
  return axios.post('/api/site/exportChangeForm', params);
};

export const downloadAssetForm = filename => {
  return downloadFile('get', `/api/site/downloadForm?filename=${filename}`);
};

export const downloadChangeForm = filename => {
  return downloadFile('get', `/api/site/downloadChangeForm?filename=${filename}`);
};

export const downloadImportFailedSitesForm = filename => {
  return downloadFile('get', `/api/site/import-failed-sites-form?filename=${filename}`);
};

export const deleteAssetForm = filename => {
  return axios.post('/api/site/deleteForm', { filename });
};

export const getAreaList = params => {
  return axios.get(`/api/site/area?${qs.stringify(params)}`);
};

export const addArea = body => {
  return axios.post('/api/site/area', body);
};

export const updateArea = body => {
  return axios.post('/api/site/area/update', body);
};

export const deleteArea = _id => {
  return axios.post('/api/site/area/delete', { _id });
};

export const getAssetCategoryList = () => {
  return axios.get('/api/site/asset-category');
};

export const addAssetCategory = body => {
  return axios.post('/api/site/asset-category', body);
};

export const updateAssetCategory = body => {
  return axios.post('/api/site/asset-category/update', body);
};

export const deleteAssetCategory = _id => {
  return axios.post('/api/site/asset-category/delete', { _id });
};

export const getAssetVulRecord = query => {
  return axios.get(`/api/site/asset-vul-record?${qs.stringify(query)}`);
};

export const getAssetStats = () => {
  return axios.get('/api/site/asset-stats');
};

export const batchUpdateAssets = body => {
  return axios.post('/api/site/batchUpdateSite', body);
};

export const getKnowledgeDetail = product => {
  return axios.get(`/api/site/knowledge-detail?product=${product}`);
};

export const getSiteChangeRecord = query => {
  return axios.get(`/api/site/site-change-record?${qs.stringify(query)}`);
};

export const getSiteChangeRecordDetail = id => {
  return axios.get(`/api/site/site-change-record/detail?id=${id}`);
};

export const changeSiteKnownStatus = id => {
  return axios.patch(`/api/site/site-change-record/detail?_id=${id}`);
};
export const getAssetIndustryList = () => {
  return axios.get('/api/site/asset-industry');
};

export const addAssetIndustry = body => {
  return axios.post('/api/site/asset-industry', body);
};

export const delAssetIndustry = body => {
  return axios.post('/api/site/asset-industry/delete', body);
};

export const updateAssetIndustry = body => {
  return axios.post('/api/site/asset-industry/update', body);
};

export const getAllIndustry = () => {
  return axios.get('/api/site/asset-all-industry');
};

export const ignoreOneday = (siteId, onedayId) => {
  return axios.post('/api/site/ignore-oneday', { siteId, onedayId });
};

export const cancelIgnoreOneday = (siteId, onedayId) => {
  return axios.post('/api/site/cancel-ignore-oneday', { siteId, onedayId });
};

export const getZoomeyeOrgList = query => {
  const params = pickBy(query, i => i || i === false);
  return axios.get(`/api/site/zoomeyeOrgAsset?${qs.stringify(params)}`);
};

export const verifyOrgIds = ids => {
  return axios.post('/api/site/zoomeyeOrgAsset/verify', { ids });
};

// 获取ip矩阵图A网段信息
export const getAMatrixList = () => {
  return axios.get('/api/site/ipMatrix/list');
};

// 获取ip矩阵图B，C网段信息
export const getBMatrixList = id => {
  return axios.get(`/api/site/ipMatrix/detail?id=${id}`);
};

// 通过ip矩阵图获取资产列表
export const getSitesByIPMatrix = id => {
  return axios.get(`/api/site/ipMatrix/sites?id=${id}`);
};
